package com.xthh.esl;

import android.content.Context;

import com.xt.xthh.R;

public class IUtils {
    /**
     * 设备信息解析
     *
     * @param info
     * @return
     */
    public static DeviceInfo loadDeviceInfo2(String info) {
        boolean color_ex = false;
        DeviceInfo deviceInfo = new DeviceInfo();
        try {
            byte[] toByte = FMUtil.hexToByte(info);
            int tagLength = 0;
            if (toByte[0] == Constants.TLV_SCREEN_INFO) {
                String manufacturer = manufacturer = "厂商-无用";
                deviceInfo.setManufacturer(String.format("%s(%02x)", manufacturer, toByte[2]).toUpperCase());
                String color = "";
                String en_color = "";
                switch (toByte[4]) {
                    case 0x20:
                        color = "BW";
                        en_color = "BW";
                        break;
                    case 0x30:
                        color = "BWR";
                        en_color = "BWR";
                        break;
                    case 0x31:
                        color = "BWRY";
                        en_color = "BWRY";
                        break;
                    default:
                        color = String.format("%d 色", toByte[4]>>4);
                        en_color = String.format("%d Color", toByte[4]>>4);
                        break;
                }
                deviceInfo.setColor(String.format("%s", color));
                deviceInfo.setEN_Color(String.format("%s", en_color));


                int x = Integer.parseInt(info.substring(10, 14), 16);
                int y = Integer.parseInt(info.substring(14, 18), 16);
                deviceInfo.setScreen(y + "x" + x);
                deviceInfo.setWidth(y);
                deviceInfo.setHeight(x);

                byte flipInfo = toByte[3];
                int hf = (flipInfo >> 7) & 0x01;
                int vf = (flipInfo >> 6) & 0x01;
                if(hf == 1){
                    deviceInfo.setFlipHorizontal(true);
                }
                if(vf == 1){
                    deviceInfo.setFlipVertical(true);
                }


            }
            tagLength = tagLength+ toByte[1+ tagLength] +2;
            if (toByte[tagLength] == Constants.TLV_COLOR_INFO ) {
                String screenScan = "";
                switch (toByte[tagLength+2]) {
                    case 0x00:
                        screenScan = "垂直扫描";
                        break;
                    case 0x01:
                        screenScan = "水平扫描";
                        break;
                    default:
                        break;
                }
                deviceInfo.setRefreshScan(toByte[tagLength+2]);
                deviceInfo.setScanType(String.format("(%s)", screenScan));
                // 23 00 30 48 FFFF
                if(0xE0 == toByte[tagLength+3])
                {//扩展参数
                    color_ex = true;
                }
                else
                {
                    int size = (toByte[tagLength+3] >> 4) & 0xff;
                    deviceInfo.setSize(size);
                    int colorCount = toByte[tagLength+3] & 0x0F;
//                int colorCount = 3;
                    deviceInfo.setColorCount(colorCount);
                    StringBuilder colorType = new StringBuilder();
                    for (int i = 0; i < colorCount; i++) {
                        String type = "";
                        byte value = (byte) ((toByte[tagLength+4 + i] >> 5) & 0xff);
                        switch (value) {
                            case 0x00:
                                int blackColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setBlack(blackColorValue );
                                type = String.format("%s(%s)", "B", blackColorValue);
                                break;
                            case 0x01:
                                int whiteColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setWhite(whiteColorValue );
                                type = String.format("%s(%s)", "W", whiteColorValue);
                                break;
                            case 0x02:
                                int redColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setRed(redColorValue );
                                deviceInfo.setDeviceType(1);
                                type = String.format("%s(%s)", "R", redColorValue);
                                break;
                            case 0x03:
                                int yellowColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setYellow(yellowColorValue );
                                deviceInfo.setDeviceType(2);
                                type = String.format("%s(%s)", "Y", yellowColorValue);
                                break;
                            default:
                                break;
                        }
                        colorType.append(type);
                    }
                    deviceInfo.setColorType(colorType.toString());
                }
            }

            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_IMAGE_COUNT) {
                deviceInfo.setPictureCapacity(toByte[tagLength+2]);
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_USER_DATA ) {
                deviceInfo.setUserData(toByte[tagLength+2]);
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_POWER_MODE ) {
                if (toByte[tagLength+2] == 0x00) {
                    deviceInfo.setBattery(false);
                } else {
                    deviceInfo.setBattery(true);
                }
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_APPID ) {
                byte[] appId = new byte[4];
                System.arraycopy(toByte, tagLength+2, appId, 0, 4);
                deviceInfo.setAppID(FMUtil.byteToHex(appId));
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_UUID ) {
                byte[] UID = new byte[4];
                System.arraycopy(toByte, tagLength+2, UID, 0, 4);
                deviceInfo.setUID(FMUtil.byteToHex(UID));
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_RFU ) {
                if (toByte[tagLength+2] == 0x00) {
                    deviceInfo.setCompress(false);
                } else {
                    deviceInfo.setCompress(true);
                }
                if(toByte[tagLength+3] == (byte) 0x20){
                    deviceInfo.setCosVersion(2);
                }
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if(toByte[tagLength] == Constants.TLV_COLOR_INFO_EX)
            {
                int size = (toByte[tagLength+2] >> 4) & 0x0f;
                deviceInfo.setSize(size);
                int colorCount = toByte[tagLength+2] & 0x0F;

                deviceInfo.setColorCount(colorCount);
                StringBuilder colorType = new StringBuilder();
                deviceInfo.setDeviceType(6);
                for (int i = 0; i < colorCount; i++) {
                    String type = "";
                    byte value = (byte) ((toByte[tagLength + 4 + i * 2]) & 0xff);
                    switch (value) {
                        case 0x00:
                            int blackColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setBlack(blackColorValue );
                            type = String.format("%s(%02X)", "B", (byte)blackColorValue);
                            break;
                        case 0x01:
                            int whiteColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setWhite(whiteColorValue );
                            type = String.format("%s(%02X)", "W", (byte)whiteColorValue);
                            break;
                        case 0x02:
                            int redColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setRed(redColorValue );
                            type = String.format("%s(%02X)", "R", (byte)redColorValue);
                            break;
                        case 0x03:
                            int yellowColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setYellow(yellowColorValue );
                            type = String.format("%s(%02X)", "Y", (byte)yellowColorValue);
                            break;
                        case 0x04:
                            int orangeColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setOrangre(orangeColorValue );
                            type = String.format("%s(%02X)", "O", (byte)orangeColorValue);
                            break;
                        case 0x05:
                            int greenColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setGreen(greenColorValue );
                            type = String.format("%s(%02X)", "G", (byte)greenColorValue);
                            break;
                        case 0x06:
                            int cyanColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setCyan(cyanColorValue );
                            type = String.format("%s(%02X)", "青", (byte)cyanColorValue);
                            break;
                        case 0x07:
                            int blueColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setBlue(blueColorValue );
                            type = String.format("%s(%02X)", "Blue", (byte)blueColorValue);
                            break;
                        case 0x08:
                            int violetColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setViolet(violetColorValue );
                            type = String.format("%s(%02X)", "紫", (byte)violetColorValue);
                            break;
                        default:
                            break;
                    }
                    colorType.append(type);
                }
                deviceInfo.setColorType(colorType.toString());
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if(toByte[toByte.length-2] == (byte)0x90 && toByte[toByte.length-1] == (byte)0x00){
                byte[] color_desc = new byte[14];
                String ColorDesc = null;
                System.arraycopy(toByte, toByte.length - 16, color_desc, 0, 14);

                if("4_color Screen".contentEquals(FMUtil.byteToString(color_desc)))
                {
                    deviceInfo.setColorDesc(FMUtil.byteToString(color_desc));

                    ColorDesc = deviceInfo.getColorDesc();
                }
                if("5345303339384E30310200000000".equals(FMUtil.byteToHex(color_desc))){
                    deviceInfo.setSpecialScreen("SE0398N01_02");
                    deviceInfo.setColorDesc("4_color Screen");
                    ColorDesc = deviceInfo.getColorDesc();
                }
                if("5345303339384E30320000000000".equals(FMUtil.byteToHex(color_desc))){
                    deviceInfo.setSpecialScreen("SE0398N02");
                }

                if(null != ColorDesc)
                {
                    if((14 == deviceInfo.getColorDesc().length())&&("4_color Screen".contentEquals(deviceInfo.getColorDesc())))
                    {
                        int x = Integer.parseInt(info.substring(10, 14), 16)/2;
                        int y = Integer.parseInt(info.substring(14, 18), 16);
                        deviceInfo.setScreen(y + "x" + x);
                        deviceInfo.setWidth(y);
                        deviceInfo.setHeight(x);
                        deviceInfo.setColor("BWRY");
                        deviceInfo.setEN_Color("4-color");
                        deviceInfo.setDeviceType(3);
                        deviceInfo.setColorCount(4);
                        deviceInfo.setBlack(0);
                        deviceInfo.setWhite(1);
                        deviceInfo.setRed(3);
                        deviceInfo.setYellow(2);
                        String type = String.format("黑(0)白(1)红(3)黄(2)");
                        deviceInfo.setColorType(type);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deviceInfo;

    }


    /**
     * 设备信息解析
     *
     * @param mContext
     * @param info
     * @return
     */
    public static DeviceInfo loadDeviceInfo(Context mContext, String info) {
        boolean color_ex = false;
        DeviceInfo deviceInfo = new DeviceInfo();
        try {
            byte[] toByte = FMUtil.hexToByte(info);
            int tagLength = 0;
            if (toByte[0] == Constants.TLV_SCREEN_INFO) {
                String manufacturer = manufacturer = "厂商-无用";
                //String manufacturer = manufacturer = UIUtils.getString(mContext, R.string.text_other);


                deviceInfo.setManufacturer(String.format("%s(%02x)", manufacturer, toByte[2]).toUpperCase());
                String color = "";
                String en_color = "";
                switch (toByte[4]) {
                    case 0x20:
                        color = UIUtils.getString(mContext, R.string.text_color_20_cn);
                        en_color = UIUtils.getString(mContext, R.string.text_color_20);
                        break;
                    case 0x30:
                        color = UIUtils.getString(mContext, R.string.text_color_30_cn);
                        en_color = UIUtils.getString(mContext, R.string.text_color_30);
                        break;
                    case 0x31:
                        color = UIUtils.getString(mContext, R.string.text_color_31_cn);
                        en_color = UIUtils.getString(mContext, R.string.text_color_31);
                        break;
                    default:
                        color = String.format("%d 色", toByte[4]>>4);
                        en_color = String.format("%d Color", toByte[4]>>4);
                        break;
                }
//                deviceInfo.setColor(String.format("%s(%02x)", color, toByte[4]));
                deviceInfo.setColor(String.format("%s", color));
                deviceInfo.setEN_Color(String.format("%s", en_color));


                int x = Integer.parseInt(info.substring(10, 14), 16);
                int y = Integer.parseInt(info.substring(14, 18), 16);
                deviceInfo.setScreen(y + "x" + x);
                deviceInfo.setWidth(y);
                deviceInfo.setHeight(x);

                byte flipInfo = toByte[3];
                int hf = (flipInfo >> 7) & 0x01;
                int vf = (flipInfo >> 6) & 0x01;
                if(hf == 1){
                    deviceInfo.setFlipHorizontal(true);
                }
                if(vf == 1){
                    deviceInfo.setFlipVertical(true);
                }


            }
            tagLength = tagLength+ toByte[1+ tagLength] +2;
            if (toByte[tagLength] == Constants.TLV_COLOR_INFO ) {
                String screenScan = "";
                switch (toByte[tagLength+2]) {
                    case 0x00:
                        screenScan = UIUtils.getString(mContext, R.string.text_screen_scan_vertical);
                        break;
                    case 0x01:
                        screenScan = UIUtils.getString(mContext, R.string.text_screen_scan_level);
                        break;
                    default:
                        break;
                }
                deviceInfo.setRefreshScan(toByte[tagLength+2]);
                deviceInfo.setScanType(String.format("(%s)", screenScan));
                // 23 00 30 48 FFFF
                if(0xE0 == toByte[tagLength+3])
                {//扩展参数
                    color_ex = true;
                }
                else
                {
                    int size = (toByte[tagLength+3] >> 4) & 0xff;
                    deviceInfo.setSize(size);
                    int colorCount = toByte[tagLength+3] & 0x0F;
//                int colorCount = 3;
                    deviceInfo.setColorCount(colorCount);
                    StringBuilder colorType = new StringBuilder();
                    for (int i = 0; i < colorCount; i++) {
                        String type = "";
                        byte value = (byte) ((toByte[tagLength+4 + i] >> 5) & 0xff);
                        switch (value) {
                            case 0x00:
                                int blackColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setBlack(blackColorValue );
                                type = String.format("%s(%s)", UIUtils.getString(mContext, R.string.text_color_black), blackColorValue);
                                break;
                            case 0x01:
                                int whiteColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setWhite(whiteColorValue );
                                type = String.format("%s(%s)", UIUtils.getString(mContext, R.string.text_color_white), whiteColorValue);
                                break;
                            case 0x02:
                                int redColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setRed(redColorValue );
                                deviceInfo.setDeviceType(1);
                                type = String.format("%s(%s)", UIUtils.getString(mContext, R.string.text_color_red), redColorValue);
                                break;
                            case 0x03:
                                int yellowColorValue = (toByte[tagLength+4 + i] >> (3 - colorCount + 3)) & (colorCount == 2 ? 1 : 3);
                                deviceInfo.setYellow(yellowColorValue );
                                deviceInfo.setDeviceType(2);
                                type = String.format("%s(%s)", UIUtils.getString(mContext, R.string.text_color_yellow), yellowColorValue);
                                break;
                            default:
                                break;
                        }
                        colorType.append(type);
                    }
                    deviceInfo.setColorType(colorType.toString());
                }
            }

            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_IMAGE_COUNT) {
                deviceInfo.setPictureCapacity(toByte[tagLength+2]);
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_USER_DATA ) {
                deviceInfo.setUserData(toByte[tagLength+2]);
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_POWER_MODE ) {
                if (toByte[tagLength+2] == 0x00) {
                    deviceInfo.setBattery(false);
                } else {
                    deviceInfo.setBattery(true);
                }
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_APPID ) {
                byte[] appId = new byte[4];
                System.arraycopy(toByte, tagLength+2, appId, 0, 4);
                deviceInfo.setAppID(FMUtil.byteToHex(appId));
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_UUID ) {
                byte[] UID = new byte[4];
                System.arraycopy(toByte, tagLength+2, UID, 0, 4);
                deviceInfo.setUID(FMUtil.byteToHex(UID));
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if (toByte[tagLength] == Constants.TLV_RFU ) {
                if (toByte[tagLength+2] == 0x00) {
                    deviceInfo.setCompress(false);
                } else {
                    deviceInfo.setCompress(true);
                }
                if(toByte[tagLength+3] == (byte) 0x20){
                    deviceInfo.setCosVersion(2);
                }
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if(toByte[tagLength] == Constants.TLV_COLOR_INFO_EX)
            {
                int size = (toByte[tagLength+2] >> 4) & 0x0f;
                deviceInfo.setSize(size);
                int colorCount = toByte[tagLength+2] & 0x0F;

                deviceInfo.setColorCount(colorCount);
                StringBuilder colorType = new StringBuilder();
                deviceInfo.setDeviceType(6);
                for (int i = 0; i < colorCount; i++) {
                    String type = "";
                    byte value = (byte) ((toByte[tagLength + 4 + i * 2]) & 0xff);
                    switch (value) {
                        case 0x00:
                            int blackColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setBlack(blackColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_black), (byte)blackColorValue);
                            break;
                        case 0x01:
                            int whiteColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setWhite(whiteColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_white), (byte)whiteColorValue);
                            break;
                        case 0x02:
                            int redColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setRed(redColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_red), (byte)redColorValue);
                            break;
                        case 0x03:
                            int yellowColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setYellow(yellowColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_yellow), (byte)yellowColorValue);
                            break;
                        case 0x04:
                            int orangeColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setOrangre(orangeColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_orange), (byte)orangeColorValue);
                            break;
                        case 0x05:
                            int greenColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setGreen(greenColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_green), (byte)greenColorValue);
                            break;
                        case 0x06:
                            int cyanColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setCyan(cyanColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_cyan), (byte)cyanColorValue);
                            break;
                        case 0x07:
                            int blueColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setBlue(blueColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_blue), (byte)blueColorValue);
                            break;
                        case 0x08:
                            int violetColorValue = toByte[tagLength + 5 + i * 2];
                            deviceInfo.setViolet(violetColorValue );
                            type = String.format("%s(%02X)", UIUtils.getString(mContext, R.string.text_color_violet), (byte)violetColorValue);
                            break;
                        default:
                            break;
                    }
                    colorType.append(type);
                }
                deviceInfo.setColorType(colorType.toString());
            }
            tagLength = tagLength+toByte[tagLength+1]+2;
            if(toByte[toByte.length-2] == (byte)0x90 && toByte[toByte.length-1] == (byte)0x00){
                byte[] color_desc = new byte[14];
                String ColorDesc = null;
                System.arraycopy(toByte, toByte.length - 16, color_desc, 0, 14);

                if("4_color Screen".contentEquals(FMUtil.byteToString(color_desc)))
                {
                    deviceInfo.setColorDesc(FMUtil.byteToString(color_desc));
                    ColorDesc = deviceInfo.getColorDesc();
                }
                if("5345303339384E30310200000000".equals(FMUtil.byteToHex(color_desc))){
                    deviceInfo.setSpecialScreen("SE0398N01_02");
                    deviceInfo.setColorDesc("4_color Screen");
                    ColorDesc = deviceInfo.getColorDesc();
                }
                if("5345303339384E30320000000000".equals(FMUtil.byteToHex(color_desc))){
                    deviceInfo.setSpecialScreen("SE0398N02");
                }

                if(null != ColorDesc)
                {
                    if((14 == deviceInfo.getColorDesc().length())&&("4_color Screen".contentEquals(deviceInfo.getColorDesc())))
                    {
                        int x = Integer.parseInt(info.substring(10, 14), 16)/2;
                        int y = Integer.parseInt(info.substring(14, 18), 16);
                        deviceInfo.setScreen(y + "x" + x);
                        deviceInfo.setWidth(y);
                        deviceInfo.setHeight(x);
                        deviceInfo.setColor(UIUtils.getString(mContext, R.string.text_color_40_cn));
                        deviceInfo.setEN_Color("4-color");
                        deviceInfo.setDeviceType(3);
                        deviceInfo.setColorCount(4);
                        deviceInfo.setBlack(0);
                        deviceInfo.setWhite(1);
                        deviceInfo.setRed(3);
                        deviceInfo.setYellow(2);
                        String type = String.format("黑(0)白(1)红(3)黄(2)");
                        deviceInfo.setColorType(type);
                    }
                }

//                deviceInfo.setPin(false);
            }
//            else if(toByte[toByte.length-2] == (byte)0x69 && toByte[toByte.length-1] == (byte)0x85) {
//                deviceInfo.setPin(true);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deviceInfo;

    }


}
